package com.flash.dailyreport.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;


/**
 * Created by hanhaiwen on 26/02/2016.
 * 基于文本的http response实体
 * reference from lego
 */
public class HttpTextResponse {
    public static final int CODE_SUCCESS = 200;
    private static final Logger log = LoggerFactory.getLogger(HttpTextResponse.class);

    private int code; // 状态码 200 400
    private String responseBody; // body
    private Header[] headers; // headers
    private byte[] responseBytes;

    public HttpTextResponse() {
    }

    public HttpTextResponse(int code, String responseBody, Header[] headers) {
        this.code = code;
        this.responseBody = responseBody;
        this.headers = headers;
    }

    public HttpTextResponse(int code, HttpMethod method) {
        this(code, method, false);
    }

    public HttpTextResponse(int code, HttpMethod method, boolean needHeaders) {
        if (!method.isRequestSent()) {
            throw new IllegalArgumentException("request尚未发送，无法获取response");
        }
        this.code = code;

        StringBuffer bf = new StringBuffer();
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
            String line = null;
            while ((line = reader.readLine()) != null) {
                bf.append(line);
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
        }

        this.responseBody = bf.toString();

        if (needHeaders) {
            this.headers = method.getResponseHeaders();
        }
    }

    public static HttpTextResponse buildFailedResponse(String code, String msg) {
        Result failApiResponse = new Result();
        failApiResponse.setCode(code);
        failApiResponse.setMsg(msg);

        HttpTextResponse response = new HttpTextResponse();
        response.setCode(500);
        response.setResponseBody(JSONObject.toJSONString(failApiResponse));
        return response;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getResponseBody() {
        return responseBody;
    }

    public void setResponseBody(String responseBody) {
        this.responseBody = responseBody;
    }

    public Header[] getHeaders() {
        return headers;
    }

    public void setHeaders(Header[] headers) {
        this.headers = headers;
    }

    public byte[] getResponseBytes() {
        return responseBytes;
    }

    public void setResponseBytes(byte[] responseBytes) {
        this.responseBytes = responseBytes;
    }

    @Override
    public String toString() {
        return "HttpTextResponse{" +
                "code=" + code +
                ", responseBody='" + responseBody + '\'' +
                ", headers=" + Arrays.toString(headers) +
                ", responseBytes=" + Arrays.toString(responseBytes) +
                '}';
    }
}
